//
//  YMYMEmoticonPageViewCell.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/29.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMYMEmoticonPageViewCell: UICollectionViewCell {

    var buttons:[YMEmoticonButton] = [YMEmoticonButton]()
    
    //定义一个属性用来接收传值
    
    var emoticon: [YMEmoticon]?{
        didSet{
            
            for bt in buttons {
                //cell的复用
                bt.hidden = true
            }
            
            for (i,value) in emoticon!.enumerate() {
                let button = buttons[i]
                button.emoticon = value
                button.hidden = false
                if value.isEmoji {
                    //代表是emoji表情
                    button.setTitle((value.code ?? "" as NSString).emoji(), forState: .Normal)
                    //防止cell的复用对Button的赋值造成影响
                    button.setImage(nil, forState: .Normal)
                    
                }else{
                    //代表是默认或者是朗香花表情
                    let image = UIImage.getEmoticonImage(value.path)

                   button.setImage(image, forState: UIControlState.Normal)
                    //防止cell的复用影响
                    button.setTitle(nil, forState: .Normal)
                }
                
            }
        }
    }
    
    
    //给信息label显示
    var indexPath:NSIndexPath?{
        
        didSet{
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addChildButtonsForCell()
        backgroundColor = RandomColor()
        //add控件
        contentView.addSubview(deleteButton)
        contentView.addSubview(messageLabel)
        //add约束
        messageLabel.snp_makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
        
        
    }
    
    
    //MARK:点击按钮的点击事件
    //表情按钮
    func buttonClick(button:YMEmoticonButton) {
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonButtonsNoti, object: button.emoticon)
    }
    
    //删除按钮点击
    func deleteButtonClick() {
        //通知textview改变text
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNoti, object: nil)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = (self.frame.size.width - 10)/CGFloat(YMEmoticonMaxCol)
        let h = (self.frame.size.height - 20)/CGFloat(YMEmoticonMaxRow)
        for (i,value) in buttons.enumerate() {
            
            
            let col = CGFloat(i%YMEmoticonMaxCol)
            let row = CGFloat(i/YMEmoticonMaxCol)
            
            value.frame = CGRectMake(w*col + 5, h*row, h, w)
            
            
        }
        //设置删除按钮
        deleteButton.frame = CGRectMake(self.frame.size.width - w - 5, h*2, w, h)
        deleteButton.backgroundColor = UIColor.whiteColor()
    }
    
    //MARK:添加20个button
    func addChildButtonsForCell() {
        
        for _ in 0..<YMEmoticonMaxCount {
            
            let button = YMEmoticonButton()
            button.addTarget(self, action: #selector(YMYMEmoticonPageViewCell.buttonClick), forControlEvents: .TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            buttons.append(button)
        }
        
        
    }
    
    //        MARK: - 懒加载控件
    private lazy var messageLabel :UILabel = {
       let lab = UILabel()
        lab.textColor = UIColor.redColor()
        lab.textAlignment = .Center
        lab.preferredMaxLayoutWidth = SCREENW
        lab.font = UIFont.systemFontOfSize(35)
        lab.numberOfLines = 0
        return lab
        
    }()
    
    private lazy var deleteButton:UIButton = {
       let button = UIButton()
        //点击事件
        button.addTarget(self, action: #selector(YMYMEmoticonPageViewCell.deleteButtonClick), forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
}
